import { useConfigStore } from '@renderer/stores/index'
import { VideoState, VideoType } from '@renderer/types'
import { ElMessage } from 'element-plus'
import { ref, toRefs } from 'vue'
import { MainProcessNoticeType } from '@renderer/types'
const isRun = ref(false)

export default () => {
  const { config } = useConfigStore()
  const video = ref<VideoType>({} as unknown as VideoType)
  const { videoSaveDirectory, files } = toRefs(config)
  function getCompressFile() {
    const file = config.files.find((file) => file.state === VideoState.READY)
    if (file) {
      file.state = VideoState.COMPRESS
      video.value = file
    }
  }
  function validate() {
    let message = ''
    if (videoSaveDirectory.value === '') message = '视频目录不能为空'
    if (files.value.filter((file) => file.state === VideoState.READY).length === 0) message = '请选择视频文件'
    if (message) {
      ElMessage.warning({ message })
      isRun.value = false
    }
    return message === ''
  }

  function run() {
    if (isRun.value) return
    isRun.value = true
    config.files.forEach((video) => {
      if (video.state === VideoState.ERROR) {
        video.state = VideoState.READY
      }
    })
    compress()
  }
  function compress() {
    config.files.forEach((video) => {
      if (video.state === VideoState.COMPRESS) {
        video.state = VideoState.READY
      }
    })
    if (!validate()) return
    getCompressFile()
    window.api.compress({
      file: { ...video.value },
      fps: Number(config.frame),
      size: config.size,
      directory: config.videoSaveDirectory,
      videoBitrate: config.videoBitrate
    })
  }
  // 监听主进程进度信息
  function progressNotice() {
    window.api.mainProcessNotice((type: MainProcessNoticeType, option: any, path: string) => {
      const video = config.files.find((video) => video.path === path)
      if (!video) return
      switch (type) {
        case MainProcessNoticeType.PROGRESS:
          video.process = option
          break
        case MainProcessNoticeType.END:
          video.state = VideoState.FINISH
          if (files.value.filter((file) => file.state === VideoState.READY).length === 0) {
            ElMessage.success('全部视频转换完成')
            isRun.value = false
          } else {
            compress()
          }
          break
        case MainProcessNoticeType.ERROR:
          video.state = VideoState.ERROR
          ElMessage.error(option.file.name + '转换失败')
          break
        case MainProcessNoticeType.DIRECTORY_CHECK:
          video.state = VideoState.READY
          isRun.value = false
          ElMessage.warning({ message: option })
          break
        case MainProcessNoticeType.STOP:
          isRun.value = false
          ElMessage.error({ message: '停止视频转换' })
          break
        case MainProcessNoticeType.FILE_IS_EXISTS:
          video.state = VideoState.FINISH
          video.process = 100
          ElMessage.error({ message: option })
          compress()
          break
      }
    })
  }
  return {
    run,
    isRun,
    progressNotice
  }
}
